﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data;
using DTO;

using Moved;

namespace BUS
{
    public class CaBUS
    {
        private static String _connString = "";
        
        static  CaBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["WebChamCongConnectionString"].ConnectionString.ToString(); 
        }

        public static double getHourWork(int ShiftID)
        {
            double Shiftcode=0;
            using (Moved_DB dc = new Moved_DB(_connString))
            {
                var GioLam = (from sh in dc.AL_Shift
                              where sh.ShiftID == ShiftID
                              select new 
                              {
                                 h= sh.WorkMinutes
                              }
                                );
                
                if (GioLam == null)
                {
                    Shiftcode = 0;
                }
                else
                {
                    foreach (var g in GioLam)
                    {
                        Shiftcode = (double)g.h;
                    }
                }
            }
            return Shiftcode;

        }
        
        public static List<CaDTO> GetAllShift()
        {
            List<CaDTO> localShifts = new List<CaDTO>();

            using (Moved_DB dc = new Moved_DB(_connString))
            {
                // Get all department in database
                var objShifts = (from shift in dc.AL_Shift
                                 select new
                                            {
                                            id = shift.ShiftID,
                                            code = shift.ShiftCode,
                                            name = shift.ShiftName,
                                            englishName = shift.EnglishName,
                                            startTime = shift.StartTime,
                                            endTime = shift.EndTime,
                                            isLate = shift.IsLate,
                                            lateMinutes = shift.LateMinutes,
                                            isEarly = shift.IsEarly,
                                            earlyMinutes = shift.EarlyMinutes,
                                            checkIn = shift.CheckIn,
                                            checkOut = shift.CheckOut,
                                            checkInTimeFrom = shift.CheckInTimeFrom,
                                            checkInTimeTo = shift.CheckInTimeTo,
                                            checkOutTimeFrom = shift.CheckOutTimeFrom,
                                            checkOutTimeTo = shift.CheckOutTimeTo,
                                            workDay = shift.WorkDay,
                                            workHalfDay = shift.WorkDayHalf,
                                            fromMinutes = shift.FromMinutes,
                                            toMinutes = shift.FromMinutes,
                                            fromMinutesHalf = shift.FromMinutesHalf,
                                            toMinutesHalf = shift.ToMinutesHalf,
                                            workMinutes = shift.WorkMinutes,
                                            mealTimeFrom = shift.MealTimeFrom,
                                            mealTimeTo = shift.MealTimeTo,
                                            totalMealTime = shift.TotalMealTime,
                                            isAddTimeIn = shift.IsAddTimeIn,
                                            isAddTimeOut = shift.IsAddTimeOut,
                                            minOverTime = shift.MinOverTime,
                                            usedOverTime = shift.UsedOverTime,
                                            maxOverTime = shift.MaxOverTime,
                                            mealOverTime = shift.MealOverTime,
                                            isOverTime = shift.IsOverTime,
                                            //checkInInDay = shift.CheckInInDay,
                                            //checkOutInDay = shift.CheckOutInDay
                                          }
                                          ).Distinct().OrderBy(item => item.id);
                
                // Each item in above list)
                foreach (var item in objShifts)
                {
                    CaDTO caItem = new CaDTO();
                    caItem.ShiffID = item.id;
                    caItem.ShiffCode = item.code;
                    caItem.ShiffName = item.name;
                    caItem.EnglishName = item.englishName;
                    caItem.StartTime = item.startTime;
                    caItem.EndTime = item.endTime;
                    caItem.IsLate = (bool) item.isLate;
                    caItem.LateMinutes = (int) item.lateMinutes;
                    caItem.IsEarly = (bool) item.isEarly;
                    caItem.EarlyMinutes = (int) item.earlyMinutes;
                    caItem.CheckIn = (bool) item.checkIn;
                    caItem.ChectOut = (bool) item.checkOut;
                    caItem.CheckInTimeFrom = (DateTime) item.checkOutTimeFrom;
                    caItem.CheckInTimeTo = (DateTime) item.checkInTimeTo;
                    caItem.CheckOutTimeFrom = (DateTime) item.checkOutTimeFrom;
                    caItem.CheckOutTimeTo = (DateTime) item.checkOutTimeTo;
                    caItem.Workday = (decimal) item.workDay;
                    caItem.WorkDayHalf = (decimal) item.workHalfDay;
                    caItem.FromMinute = (int) item.fromMinutes;
                    caItem.ToMinutes = (int) item.toMinutes;
                    caItem.FromMinutesHalf = (int) item.fromMinutesHalf;
                    caItem.ToMinutesHalf = (int) item.toMinutesHalf;
                    caItem.WorkMinutes = (int) item.workMinutes;
                    caItem.MealTimeFrom = (DateTime) item.mealTimeFrom;
                    caItem.MealTimeTo = (DateTime) item.mealTimeTo;
                    caItem.TotalMealTime = (int) item.totalMealTime;
                    caItem.IsAddTimeIn = (bool) item.isAddTimeIn;
                    caItem.IsAddTimeOut = (bool) item.isAddTimeOut;
                    caItem.MinOverTime = (int) item.minOverTime;
                    caItem.UsedOverTime = (bool) item.usedOverTime;
                    caItem.MaxOverTime = (int) item.maxOverTime;
                    caItem.MealOverTime = (int) item.mealOverTime;
                    caItem.IsOverTime = (bool) item.isOverTime;
                    //caItem.CheckInInDay = (bool) item.checkInInDay;
                    //caItem.CheckOutInDay = (bool) item.checkOutInDay;
                    localShifts.Add(caItem);
                }

            }

            return localShifts;
        }
        
    }
}
